import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { UnitsProvider } from '../../providers/units/units';
/**
 * Generated class for the AssistantlixiangPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-assistantlixiang',
  templateUrl: 'assistantlixiang.html',
})
export class AssistantlixiangPage {
	/**
	 * 输入界面控制
	 */
	inputSelectView = {
		applyoaamountDefItems: new Array(), //费用分类
  }
  /**
	 * 输入界面控制
	 */
	inputViewModel = {
		applyapplymember: '', //申请人
    applytime: '', //申请日期
    applycompany: new Object(), //申请公司
		applydepartment: new Object(), //申请部门
		applyapplytitle: '', //项目名称
		applyapplyamount: '', //预计所需金额
    applydemainpartment: new Object(), //主办部门
    applydesubpartment: new Object(), //协办部门
		applychangenote: '', //申请理由
		applyamounttype: '', //费用分类
    applyahandlemember: '', //经办人
		//
		applynote: '', //备注
		applyuploadfile: '', //附件
		applynownode: '', //当前节点
		applynowmember: '', //当前处理人
		applynextnode: '', //下一步审批节点
		applynextman: '', //下一步审批人
	}

	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider,
		public units: UnitsProvider) {
	}

  ionViewDidLoad() {
    console.log('ionViewDidLoad AssistantlixiangPage');
    this.ionicLoad();
  }

  //当前节点
  ionicLoad() {
    //控制显示
    this.getUserInfo();
    this.loadaoamountDefData();
    this.inputViewModel.applytime = this.units.getCurrentDate(new Date().getTime());
    this.inputViewModel.applynownode = '发起立项申请'; //当前节点
    this.inputViewModel.applynextnode = '部门领导审批'; //当前节点
  }
  //获取本用户信息
  getUserInfo() {
    this.storage.getStorage("user").then(val => {
      //请求方式如下
      let params = {
        user_id: val['user_id']
      };
      this.http.postJson('user', 'douserinfo.do', params).then(res => {
        if (res['success'] == true) {
          let info = res['data'];
          //请求成功
          this.inputViewModel.applyapplymember = info['name'];
          this.inputViewModel.applynowmember = info['name'];
        } else {
          this.dialog.showToast("top", res['msg']);
        }
      });
    });
  }
	//初始化费用分类
	loadaoamountDefData() {
		//初始化
		let params = new Object();
		params['type'] = 'oa_projectApproval_feeType';
		this.http.postJson('dict', 'getDictByType.do', params).then(res => {
			let datas = res['data']['dictList'];
			this.inputSelectView.applyoaamountDefItems = new Array();
			for (let i = 0; i < datas.length; i++) {
				let item = datas[i];
				this.inputSelectView.applyoaamountDefItems.push({
					id: item['id'],
					name: item['value']
				});
			}
		});
	}
  //初始化选择公司
  applycompanyselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applycompany;
    data['multiselect'] = false; //false 单选 true 多选
    let opts = new Object();
    this.dialog.showModal("ModalcompanyselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applycompany = result[0];
      }
    });
  }
  //初始化选择部门
  applydepartselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applydepartment;
    data['multiselect'] = false; //false 单选 true 多选
    let opts = new Object();
    this.dialog.showModal("ModaldepartselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applydepartment = result[0];
      }
    });
  }
  //初始化选择用户
  applyapplymemberselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applynextman;
    data['multiselect'] = false; //false 单选 true 多选
    let opts = new Object();
    this.dialog.showModal("ModaluserselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applynextman = result[0];
      }
    });
  }
  //变更类型对比选中
  compareselect(e1: Object, e2: Object) {
    return e1 && e2 ? e1['id'] === e2['id'] : e1 === e2;
  }

  //提交数据
  applyapplydone() {
    if (this.checkInput()) {
      //初始化
      this.storage.getStorage("user").then(val => {
        //请求方式如下
        let params = {
          'user_id': val['user_id'],
          'actionFlag': 'yes',// 操作标识 必填 yes
          'feeType': this.inputViewModel.applyamounttype,// 费用分类 必填 字典表 
          'goodsType': val['user_id'],// 项目名称 必填
          'name': val['user_id'],// 预计所需金额（元） 必填
          'requiredMoney': val['user_id'],// 主办部门(ID) 非必填 
          'sponsorDept.id': val['user_id'],// 协办部门(id) 非必填
          'coDept.id': val['user_id'],// 申请理由 必填
          'applyReason': val['user_id'], // 申请公司(名称) 必填
          'company': val['user_id'],// 申请部门（ID） 必填
          'agent.id': val['user_id'],// 经办人（ID）   必填
          'remarks': val['user_id'],// 备注 非必填
          'auditPersonId': val['user_id'],// 下一步审批人（ID） 必填
        };
        //收文传阅列表
        this.http.postJson('oa', 'projectApproval/insert.do', params).then(res => {
          if (res['data'] != undefined) {

          } else {

          }
        });
      });
    }
  }

  //检查输入
  checkInput() {
   
    return true;
  }
}
